<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">编码</label>
        <el-input
          v-model="query.code"
          clearable
          placeholder="编码"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">名称</label>
        <el-input
          v-model="query.name"
          clearable
          placeholder="名称"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">区域</label>
        <el-select v-model="query.area" filterable placeholder="请选择" style="width: 185px;" class="filter-item">
          <el-option v-for="item in dict.customer_area" :key="item.id" :label="item.label" :value="item.value" />
        </el-select>
        <label class="el-form-item-label">类别</label>
        <el-select
          v-model="query.type"
          filterable
          placeholder="请选择"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        >
          <el-option v-for="item in dict.customer_type" :key="item.id" :label="item.label" :value="item.value" />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="500px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="编码" prop="code">
            <el-input v-model="form.code" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="类别" prop="area">
            <el-select v-model="form.type" filterable placeholder="请选择">
              <el-option v-for="item in dict.customer_type" :key="item.id" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="区域" prop="area">
            <el-select v-model="form.area" filterable placeholder="请选择">
              <el-option v-for="item in dict.customer_area" :key="item.id" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remark" :rows="3" type="textarea" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="code" label="编码" />
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="area" label="区域">
          <template slot-scope="scope">
            {{ dict.label.customer_area[scope.row.area] }}
          </template>
        </el-table-column>
        <el-table-column prop="type" label="类别">
          <template slot-scope="scope">
            {{ dict.label.customer_type[scope.row.type] }}
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" />
        <el-table-column
          v-if="checkPer(['admin', 'customer:edit', 'customer:del'])"
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation :data="scope.row" :permission="permission" />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
/** 租户管理 */
import crudCustomer from '@/api/jf/customer'
import CRUD, { crud, form, header, presenter } from '@crud/V2/crud'
import crudOperation from '@crud/V2/CRUD.operation'
import pagination from '@crud/V2/Pagination'
import rrOperation from '@crud/V2/RR.operation'
import udOperation from '@crud/V2/UD.operation'

const defaultForm = {
  code: null,
  name: null,
  area: null,
  remark: null,
  createBy: null,
  updateBy: null,
  createTime: null,
  updateTime: null,
  id: null
}
export default {
  name: 'Customer',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['customer_area', 'customer_type'],
  cruds() {
    return CRUD({
      title: '租户',
      url: 'api/v2/base/customer',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: { ...crudCustomer }
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'customer:add'],
        edit: ['admin', 'customer:edit'],
        del: ['admin', 'customer:del']
      },
      rules: {
        code: [{ required: true, message: '编码不能为空', trigger: 'blur' }],
        name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
        area: [{ required: true, message: '区域不能为空', trigger: 'blur' }],
        type: [{ required: true, message: '类别不能为空', trigger: 'blur' }],
        id: [{ required: true, message: '不能为空', trigger: 'blur' }]
      },
      queryTypeOptions: [
        { key: 'code', display_name: '编码' },
        { key: 'name', display_name: '名称' },
        { key: 'type', display_name: '类别' },
        { key: 'area', display_name: '区域' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }
  }
}
</script>

<style scoped></style>
